import brandList from './components/brand-list.vue'
// 格式化金额
function numberFormat (value) {
  let param = {}
  let k = 10000
  let sizes = ['元', '万', '亿', '万亿']
  let i
  if(!value) return ''
  if(value < k) {
    param.value =value
    param.unit = ''
  } else {
    i = Math.floor(Math.log(value) / Math.log(k))
    param.value = ((value / Math.pow(k, i))).toFixed(2)
    param.unit = sizes[i]
  }
  return param
}
export default {
  components: {
    brandList
  },
  data() {
    return {
      storeId: '', // 店铺id
      options: [
        {label: '全部', type: 0},
        {label: '品牌', type: 1}
      ],
      options1: [
        {label: '1成首付', value: 1, selected: false},
        {label: '以租代售', value: 2, selected: false},
        {label: '巨能降', value: 3, selected: false}
      ],
      downStatus: false,
      screeningCriteria: [], // 已选条件
      loading: false, // 请求状态
      isEnd: false, // 是否到最后一页
      status: 'loading', // 底部加载状态
      menuOffsetTop: 0,
      query: {
        storeid: '', // 店铺id
        brand_id: 0, // 品牌id
        PayType: '', // 1、一成首付 2、以租代售 3、巨能降
        pageIndex: 1, // 页码
        pageSize: 20 ,// 每页多少条
      }, // 请求参数
      result: {
        store: {}, // 店铺信息
        cars: [] // 车辆列表
      },
      skeletonLoading: true,
      loaction: {
        dlon: 0,
        dlat: 0
      },
      storeDefault: 'https://files.yzsheng.com/client/store/store_default.png'
    }
  },
  filters: {
    // 格式化店铺类型
    formatStoreType(data){
      let text = ''
      switch(Number(data)) {
      case 5:
        text = '旗舰店'
        break
      case 2:
        text = '体验店'
        break
      case 3:
        text = '中心店'
        break
      case 4:
        text = '4S店'
        break
      default:
        text = '旗舰店'
        break
      }
      return text
    },
    // 格式化标签
    formatTags(data) {
      let text = ''
      switch(data) {
      case 1:
        text = '以租代售'
        break
      case 2:
        text = '一成首付'
        break
      case 3:
        text = '巨能降'
        break
      default:
        text = '自营'
        break
      }
      return text
    },
    formatPrice (data) {
      if(!data) return '0'
      let res = numberFormat(data)
      return res.value + res.unit
    },
    // 格式化单位
    formatUnit(data) {
      if(!data) return '元'
      let res = numberFormat(data)
      return res.unit
    },
    // 格式化金额
    formatNum(data) {
      if(!data) return '0'
      let res = numberFormat(data)
      return res.value
    }
  },
  onLoad(option) {
    // 通过query设置店铺id
    this.storeId = option.storeid || ''
    this.query.storeid = option.storeid

    uni.showLoading({
      title: '加载中'
    })
    this.$commen.getLocation(data => {
      this.loaction.dlon = data.lon
      this.loaction.dlat = data.lat
      this.getStoreInfo(() => {
        this.$nextTick(() => {
          // 获取菜单距离顶部的距离
          uni.createSelectorQuery().select('.menu-tab').boundingClientRect(data => {
            this.menuOffsetTop = data.top
          }).exec()
        })

        this.getList(this.loaction)
      })
    })
  },
  options: { styleIsolation: 'shared' },
  methods: {
    // 点击导航
    onClickNavigation() {
      uni.openLocation({
        latitude: this.result.store.lat,
        longitude: this.result.store.lon
      })
    },
    // 点击品牌
    onChangeDropDown() {
      this.downStatus = true
      uni.pageScrollTo({
        scrollTop: this.menuOffsetTop,
        duration: 100
      })
    },
    // 品牌选择
    onClickBrand(data){
      let sc = this.screeningCriteria
      for(let i = 0; i < sc.length; i++){
        if(sc[i].type === 1) {
          sc.splice(i, 1)
          break
        }
      }
      sc.push({
        name: data.brand,
        type: 1
      })
      this.$refs.uDropdown[0].close()
      uni.pageScrollTo({
        scrollTop: 0,
        duration: 100
      })
      this.getList({
        brand_id: data.bid,
        PayType: this.query.PayType
      })
    },
    // 获取店铺详情
    async getStoreInfo(callback) {
      try{
        let res = await this.$api.store.GetCarStoreDetail({
          storeid: this.storeId,
          ...this.loaction
        })
        if(res.code === 200) {
          this.result.store = res.data
          callback && callback()
        } else {
          uni.hideLoading()
          uni.showToast({
            icon: 'none',
            title: res.msg
          })
        }
      }catch(e){
        uni.hideLoading()
      }
    },
    /*
      获取店铺新车列表
      @param {Boolean} isInit 是否初始化query数据
      params:
    */
    async getList(params) {
      try{
        if(params) {
          let p = {
            storeid: this.storeId,
            brand_id: 0,
            PayType: '',
            pageIndex: 1,
            pageSize: 20,
            is_down: false,
            is_loans: false,
            is_rent: false
          }
          this.query = Object.assign(p, this.loaction, params)
          this.isEnd = false
          this.status = 'loading'
          this.loading = false
        }
        if (this.loading || this.isEnd) return false
        this.loading = true
        let parameter = JSON.parse(JSON.stringify(this.query))
        if(parameter.PayType === 1) {
          parameter.is_down = true
        }
        if(parameter.PayType === 2) {
          parameter.is_rent = true
        }
        if(parameter.PayType === 3) {
          parameter.is_loans = true
        }

        let res = await this.$api.store.GetCarList(parameter)
        uni.hideLoading()

        if(res.code === 200) {
          let resultList = params || this.query.pageIndex === 1 ? [] : this.result.cars
          let list = res.data.list

          if (list.length > 0) {
            this.result.cars = resultList.concat(list)
            this.query.pageIndex = this.query.pageIndex + 1
            if(list.length < 20) {
              this.isEnd = true
              this.status = 'nomore'
            }
          } else {
            this.result.cars = resultList.concat(list)
            this.isEnd = true
            this.status = 'nomore'
          }
        }
        this.loading = false
        this.skeletonLoading = false
      }catch(e){
        uni.hideLoading()
        this.loading = false
        this.skeletonLoading = false
      }
    },
    // 删除筛选条件
    onClickDelScreeningCriteria(data, index) {
      let sc = this.screeningCriteria
      sc.splice(index, 1)
      this.screeningCriteria = sc
      if(data.type === 1) {
        this.query.brand_id = 0
      }
      if(data.type === 0) {
        let option = this.options1
        option.map(item => {
          item.selected = false
          return data
        })

        this.options1 = option
        this.query.PayType = ''
      }
      this.getList({
        brand_id: this.query.brand_id,
        PayType: this.query.PayType
      })
    },
    // 点击菜单筛选
    onClickMenu(data) {
      if(data.selected) return false
      let option = this.options1
      option.map(item => {
        item.selected = item.value === data.value
        return data
      })

      this.options1 = option
      let sc = this.screeningCriteria
      for(let i = 0; i < sc.length; i++){
        if(sc[i].type === 0) {
          sc.splice(i, 1)
          break
        }
      }
      sc.push({
        name: data.label,
        type: 0
      })

      uni.pageScrollTo({
        scrollTop: 0,
        duration: 100
      })
      this.getList({
        PayType: data.value,
        brand_id: this.query.brand_id
      })
    },
    // 点击重置
    onClickReset() {
      if(this.screeningCriteria.length <= 0) return false
      this.screeningCriteria = []
      this.options1.map(item => {
        item.selected = false
      })
      uni.pageScrollTo({
        scrollTop: 0,
        duration: 100
      })
      this.getList({})
    },
    // 点击关注店铺
    async onClickFollowStore() {
      if(!uni.getStorageSync('token')) {
        uni.showToast({
          icon: 'none',
          title: '请登录'
        })
        return false
      }
      // 如果已关注调用取消关注接口，否则调用关注接口
      let result
      let params = {
        favorite_type: 4, // 收藏类型 2、新车 3、保养店铺 4、车辆店铺
      }
      if(this.result.store.is_favorite) {
        params.id = this.result.store.fid
      } else {
        params.carstore_id = this.storeId
      }
      uni.showLoading({
        title: '加载中',
        mask: true
      })
      try{
        if(this.result.store.is_favorite) {
          result = await this.$api.store.cancelFavorite(params)
        } else {
          result = await this.$api.store.addFavorite(params)
        }
        uni.hideLoading()
        if(result.code === 200) {
          this.result.store.is_favorite = !this.result.store.is_favorite
        } else {
          uni.showToast({
            icon: 'none',
            title: result.msg
          })
        }
      }catch(e){
        uni.hideLoading()
        uni.showToast({
          icon: 'none',
          title: '网络请求错误'
        })
      }
      // this.result.store.isfollow = !this.result.store.isfollow
    },
    // 跳转车子详情
    onJumpDetails(item) {
      if(item.is_loans === 1) {
        uni.navigateTo({
          url: `/pages_commodity/giantnengjiangDetails/giantnengjiangDetails?goodid=${item.newcar_id}`
        })
      } else {
        uni.navigateTo({
          url: `/pages_commodity/sourceDetails/sourceDetails?goodid=${item.newcar_id}`
        })
      }
    }
  },
  onReachBottom() {
    this.getList()
  },
  unload() {
    uni.hideLoading()
  }
}